package work.day07work;

import java.util.Arrays;

public class Test07 {
    public static void main(String[] args) throws InterruptedException {
        Goo goo = new Goo();
        Thread t1 = new Thread(){
            @Override
            public void run(){
                synchronized (goo){
                    for (int i = 0; i < 100; i++) {
                        goo.array = Arrays.copyOf(goo.array, goo.array.length+ 1);
                        goo.array[goo.array.length - 1] = i;
                    }
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run(){
                synchronized (goo){
                    for (int i = 100; i < 200; i++) {
                        goo.array = Arrays.copyOf(goo.array, goo.array.length+ 1);
                        goo.array[goo.array.length - 1] = i;
                    }
                }
            }
        };
        t1.start();
        t2.start();
        Thread.sleep(1000);
        System.out.println(goo.array.length);
        System.out.println(Arrays.toString(goo.array));
    }
}

class Goo{
    public int[] array = {};
}